extends KinematicBody2D

const AutoRotate = true # Autorotate player to move direction
const DefaultSpeedValue = 200 # Player speed value
var Speed = DefaultSpeedValue # Player speed value (for processing)
var Velocity = Vector2() # Player velocity
var Health = 60 # Player's health
var Power = 100  # Player's power
var second_jump = true # Flag for double jump (Is allow second jump?)
var release_up = true # Flag for double jump (The space was released?)
var is_squat = false # Is squat state? 
var allow_power = true
var shot_button_release = true # Flag for shot key
const Floor = Vector2(0, -1) # Floor 
const Gravity = 2500 # Gravity value
const JumpPowerUp = 800 # Jump power value
const ShiftPower = 2.5 # Shift turbo value
const SitPower = -150 # Sit down power

# Load Plasma
const Plasma = preload("res://assets/Bullet/Bullet.tscn")

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func step():
	if Input.is_action_pressed("ui_left"):
		Velocity.x = -Speed
		if AutoRotate:
			$AnimatedSprite.flip_h = true
		$AnimatedSprite.play("Walk")
	elif Input.is_action_pressed("ui_right"):
		Velocity.x = Speed
		if AutoRotate:
			$AnimatedSprite.flip_h = false
		$AnimatedSprite.play("Walk")
	else:
		Velocity.x = 0
		Speed = DefaultSpeedValue
		$AnimatedSprite.stop()
		$AnimatedSprite.play("posible")

func jump():
	# Jump buttons processing
	if Input.is_action_pressed("ui_up"):
		if is_on_floor():
			Velocity.y = -JumpPowerUp
			$AnimatedSprite.play("jump")
			release_up = false
		elif second_jump and release_up:
			Velocity.y -= JumpPowerUp
			second_jump = false
			release_up = false

func run_and_squat():
	# Run
	if Input.is_action_pressed("ui_acceleration") and \
		!is_squat and Power > 0 and allow_power:
		Speed = DefaultSpeedValue * ShiftPower
	# Squat
	elif Input.is_action_pressed("ui_squat"):
		Speed = DefaultSpeedValue + SitPower
		$AnimatedSprite.play("SquatAction")
		$Collision.set_disabled(true)
		$CollisionSit.set_disabled(false)
		is_squat = true
	else:
		Speed = DefaultSpeedValue
		$Collision.set_disabled(false)
		$CollisionSit.set_disabled(true)
		is_squat = false
	if Power == 0:
		$RecoveryPower.start()
		allow_power = false

func reset_flags():
	# Reset flags 
	if is_on_floor():
		second_jump = true
	if !Input.is_action_pressed("ui_up"):
		release_up = true
	if !Input.is_action_pressed("ui_shot"):
		shot_button_release = true

func shot_check():
	# Shot key proccesing
	if Input.is_action_pressed("ui_shot") and shot_button_release:
		var plasma = Plasma.instance()
		plasma.position.x = $GunPosition.global_position.x
		plasma.position.y = $GunPosition.global_position.y
		if $AnimatedSprite.flip_h: # Left player direction
			plasma.SPEED = -plasma.SPEED
		get_parent().add_child(plasma)
		shot_button_release = false

func add_health(quantity):
	if Health < 100:
		Health += quantity

func _physics_process(_delta):
	step()
	jump()
	run_and_squat()
	shot_check()
	reset_flags()
	# Calculate the physics and move the player 
	Velocity.y += (Gravity * _delta)
	Velocity = move_and_slide(Velocity, Floor)

func _on_RunTimer_timeout():
	if Speed == DefaultSpeedValue * ShiftPower:
		Power -= 25
	elif Power < 100:
		Power += 20

func _on_RecoveryPower_timeout():
	allow_power = true
	$RecoveryPower.stop()
